<template>
	<view class='productSort productList' style="height: 100vh; display: flex; flex-direction: column">
		<view class='header acea-row row-center-wrapper'>
			<view class='acea-row row-between-wrapper input'>
				<text class='iconfont icon-sousuo'></text>
				<input type='text' placeholder='点击搜索商品信息' @confirm="searchSubmitValue" confirm-type='search'
					name="search" placeholder-class='placeholder'></input>
			</view>
		</view>

    <view class="content" style="flex: 1; height: 0; display: flex; flex-direction: row;">
      <view class='aside' style="height: 100%; width: 180rpx; box-sizing: border-box">
        <scroll-view scroll-y="true" scroll-with-animation='true' style="height: 100%; width: 100%">
          <view class='item acea-row row-center-wrapper' :class='index==navActive?"on":""'
                v-for="(item,index) in navlist" :key="index" @click='tap(item, index)'>
            <text>{{item.name}}</text>
          </view>
        </scroll-view>

      </view>

      <view class="conter" style="flex: 1; height: 100%; width: 0; overflow-y: hidden;">
        <scroll-view v-if="productList.length>0" scroll-y="true" :scroll-into-view="toView" style="height: 100%"
                     @scroll="scroll" @scrolltolower="loadMore" scroll-with-animation='true'>
            <view class='list acea-row row-between-wrapper' :class='is_switch==true?"":"on"'>
              <view class='item' :class='is_switch==true?"":"on"' hover-class='none'
                    v-for="(item,index) in productList" :key="index" @click="godDetail(item)">
                <view class='pictrue' :class='is_switch==true?"":"on"'>
                  <image :src='item.image' :class='is_switch==true?"":"on"'></image>
                  <span class="pictrue_log_class"
                        :class="is_switch === true ? 'pictrue_log_big' : 'pictrue_log'"
                        v-if="item.activityH5 && item.activityH5.type === '1'">秒杀</span>
                  <span class="pictrue_log_class"
                        :class="is_switch === true ? 'pictrue_log_big' : 'pictrue_log'"
                        v-if="item.activityH5 && item.activityH5.type === '2'">砍价</span>
                  <span class="pictrue_log_class"
                        :class="is_switch === true ? 'pictrue_log_big' : 'pictrue_log'"
                        v-if="item.activityH5 && item.activityH5.type === '3'">拼团</span>
                </view>
                <view class='text' :class='is_switch==true?"":"on"'>
                  <view class='name line1'>{{item.storeName}}</view>
                  <view class='money font-color' :class='is_switch==true?"":"on"'>￥<text
                      class='num'>{{item.price}}</text></view>
                  <view class='vip acea-row row-between-wrapper' :class='is_switch==true?"":"on"'>
                    <view class='vip-money' v-if="item.vip_price && item.vip_price > 0">
                      ￥{{item.vip_price}}
                      <image src='../../static/images/vip.png'></image>
                    </view>
                    <view>已售{{Number(item.sales) + Number(item.ficti) || 0}}{{item.unitName}}</view>
                  </view>
                </view>
            </view>
            <view class='loadingicon acea-row row-center-wrapper' v-if='productList.length > 0'>
              <text class='loading iconfont icon-jiazai' :hidden='loading==false'></text>{{loadTitle}}
            </view>
          </view>
        </scroll-view>
        <view class='noCommodity' v-if="productList.length <= 0">
          <view class='pictrue'>
            <image src='../../static/images/noShopper.png'></image>
          </view>
        </view>
      </view>
    </view>



	</view>
</template>

<script>
	import {
		getCategoryList,
		getProductslist,
	} from '@/api/store.js';
	import {
		mapGetters
	} from "vuex";
	import ClipboardJS from "@/plugin/clipboard/clipboard.js";
	import {
		goShopDetail
	} from '@/libs/order.js'

	export default {
		computed: mapGetters(['uid']),
		data() {
			return {
				navlist: [],
				productList: [],
				navActive: 0,
				number: "",
				height: 0,
				hightArr: [],
				toView: "",
				tabbarH: 0,
				where: {
					keyword: '',
					page: 1,
					limit: 20,
					cid: 743,
				},
				loadend: false,
				loading: false,
				loadTitle: '加载更多',
			}
		},
		onLoad(options) {
			this.getAllCategory()
			// console.log(options)
			// this.$set(this.where, 'cid', options.cid || 0);
			// if (optinos.cid) {
			// 	this.getAllCategory('index');
			// } else {
			// 	this.getAllCategory()
			// }
			
		},
		onShow() {},
		methods: {
			infoScroll: function() {
				let that = this;
				let len = that.navlist.length;
				this.number = len || 0;

				//设置商品列表高度
				uni.getSystemInfo({
					success: function(res) {
						console.log(res)
						that.height = (res.windowHeight) * (750 / res.windowWidth) - 98;
            console.log('this.height', that.height)
					},
				});
				let height = 0;
				let hightArr = [];
				// for (let i = 0; i < len; i++) {
				// 	//获取元素所在位置
				// 	let query = uni.createSelectorQuery().in(this);
				// 	let idView = "#b" + i;
				// 	query.select(idView).boundingClientRect();
				// 	query.exec(function(res) {
				// 		let top = res[0].top;
				// 		hightArr.push(top);
				// 		that.hightArr = hightArr
				// 	});
				// };
			},
			tap: function(item, index) {
				this.navActive = index;
				this.where.cid = item.id
				this.where.page = 1
				this.loadend = false
				console.log('tap', this.where)
				this.get_product_list(true)
			},
			getAllCategory: function() {
				let that = this;
				getCategoryList().then(res => {
					const category = res.data.find((e) => e.name === '商品分类');
					that.navlist = category.child || []
					this.where.page = 1
					this.get_product_list(true)
					setTimeout(function() {
						that.infoScroll();
					}, 500)
				})
			},
			scroll: function(e) {
				let scrollTop = e.detail.scrollTop;
				let scrollArr = this.hightArr;
				for (let i = 0; i < scrollArr.length; i++) {
					if (scrollTop >= 0 && scrollTop < scrollArr[1] - scrollArr[0]) {
						this.navActive = 0
					} else if (scrollTop >= scrollArr[i] - scrollArr[0] && scrollTop < scrollArr[i + 1] - scrollArr[
							0]) {
						this.navActive = i
					} else if (scrollTop >= scrollArr[scrollArr.length - 1] - scrollArr[0]) {
						this.navActive = scrollArr.length - 1
					}
				}
			},
			searchSubmitValue: function(e) {
				if (this.$util.trim(e.detail.value).length > 0)
					uni.navigateTo({
						url: '/pages/goods_list/index?searchValue=' + e.detail.value
					})
				else
					return this.$util.Tips({
						title: '请填写要搜索的产品信息'
					});
			},
			// 去详情页
			godDetail(item) {
				goShopDetail(item, this.uid).then(res => {
					uni.navigateTo({
						url: `/pages/goods_details/index?id=${item.id}`
					})
				})
			},
			//查找产品
			get_product_list: function(isPage) {
				console.log('get_product_list', isPage)
				let that = this;
				if (that.loadend) return;
				if (that.loading) return;
				if (isPage === true) that.$set(that, 'productList', []);
				that.loading = true;
				that.loadTitle = '';
				getProductslist(that.where).then(res => {
					let list = res.data.list;
					let productList = that.$util.SplitArray(list, that.productList);
					let loadend = list.length < that.where.limit;
					that.loadend = loadend;
					that.loading = false;
					that.loadTitle = loadend ? '已全部加载' : '加载更多';
					that.$set(that, 'productList', productList);
					that.$set(that.where, 'page', that.where.page + 1);
					// if (that.productList.length === 0) {
					// 	this.get_host_product();
					// }
					console.log('getProductslist', this.productList)
				}).catch(err => {
					that.loading = false;
					that.loadTitle = '加载更多';
				});
			},

      loadMore() {
        if (this.productList.length > 0) {
          this.get_product_list();
        } else {
          this.get_host_product();
        }
      },

			// onReachBottom() {
			// 	console.log('fsdfsdf')
			//
			// }

		}
	}
</script>

<style scoped lang="scss">
	.productSort .header {
		width: 100%;
		height: 96rpx;
		background-color: #fff;
		//position: fixed;
		//left: 0;
		//right: 0;
		//top: 0;
		z-index: 9;
		border-bottom: 1rpx solid #f5f5f5;
	}

	.productSort .header .input {
		width: 700rpx;
		height: 60rpx;
		background-color: #f5f5f5;
		border-radius: 50rpx;
		box-sizing: border-box;
		padding: 0 25rpx;
	}

	.productSort .header .input .iconfont {
		font-size: 26rpx;
		color: #555;
	}

	.productSort .header .input .placeholder {
		color: #999;
	}

	.productSort .header .input input {
		font-size: 26rpx;
		height: 100%;
		width: 597rpx;
	}

	.productSort .aside {
		//position: fixed;
		//width: 180rpx;
		//left: 0;
		//top: 0;
		background-color: #f7f7f7;
		//overflow-y: scroll;
		//overflow-x: hidden;

		//height: auto;
		//margin-top: 96rpx;
	}

	.productSort .aside .item {
		height: 100rpx;
		width: 100%;
		font-size: 26rpx;
		color: #424242;
	}

	.productSort .aside .item.on {
		background-color: #fff;
		border-left: 4rpx solid #0d5899;
		width: 100%;
		text-align: center;
		color: #005196;
		font-weight: bold;
	}

	.productSort .conter {
		margin: 0;
		padding: 0 14rpx;
		background-color: #fff;
	}

	.productSort .conter .listw {
		//padding-top: 20rpx;
	}

	.productSort .conter .listw .title {
		height: 90rpx;
	}

	.productSort .conter .listw .title .line {
		width: 100rpx;
		height: 2rpx;
		background-color: #f0f0f0;
	}

	.productSort .conter .listw .title .name {
		font-size: 28rpx;
		color: #333;
		margin: 0 30rpx;
		font-weight: bold;
	}

	.productSort .conter .list {
		flex-wrap: wrap;
	}

	.productSort .conter .list .item {
		width: 177rpx;
		margin-top: 26rpx;
	}

	.productSort .conter .list .item .picture {
		width: 120rpx;
		height: 120rpx;
		border-radius: 50%;
	}

	.productSort .conter .list .item .picture image {
		width: 100%;
		height: 100%;
		border-radius: 50%;

		div {
			background-color: #f7f7f7;
		}
	}

	.productSort .conter .list .item .name {
		font-size: 24rpx;
		color: #333;
		height: 56rpx;
		line-height: 56rpx;
		width: 120rpx;
		text-align: center;
	}


	.listBox {
		padding: 20px 15px;
		margin-top: 154rpx;
	}

	.productList .list {
		padding: 0 30rpx;
		margin-top: 192rpx;

	}

	.productList .list.on {
		border-radius: 14rpx;
		margin-top: 0 !important;
		background-color: #f5f5f5;
		padding: 16rpx;
		// margin: 20rpx 0;
		// background-color: #fff;
	}

	.productList .list .item {
		width: 335rpx;
		background-color: #fff;
		border-radius: 14rpx;
		margin-bottom: 20rpx;
	}

	.productList .list .item.on {
		box-sizing: border-box;
		width: 100%;
		display: flex;
		padding: 24rpx 24rpx 30rpx 24rpx;
		margin: 8rpx 0;
		border-radius: 14rpx;
	}

	.productList .list .item .pictrue {
		position: relative;
		width: 100%;
		height: 335rpx;
	}

	.productList .list .item .pictrue.on {
		width: 240rpx;
		height: 180rpx;
	}

	.productList .list .item .pictrue image {
		width: 100%;
		height: 100%;
		border-radius: 20rpx 20rpx 0 0;
	}

	.productList .list .item .pictrue image.on {
		border-radius: 6rpx;
	}

	.productList .list .item .text {
		padding: 18rpx 20rpx;
		font-size: 30rpx;
		color: #222;
	}

	.productList .list .item .text.on {
		width: 456rpx;
		padding: 0 0 0 20rpx;
	}

	.productList .list .item .text .money {
		font-size: 26rpx;
		font-weight: bold;
		margin-top: 8rpx;
	}

	.productList .list .item .text .money.on {
		margin-top: 30rpx;
	}

	.productList .list .item .text .money .num {
		font-size: 34rpx;
	}

	.productList .list .item .text .vip {
		font-size: 22rpx;
		color: #aaa;
		margin-top: 7rpx;
	}

	.productList .list .item .text .vip.on {
		margin-top: 12rpx;
	}

	.productList .list .item .text .vip .vip-money {
		font-size: 24rpx;
		color: #282828;
		font-weight: bold;
	}

	.productList .list .item .text .vip .vip-money image {
		width: 46rpx;
		height: 21rpx;
		margin-left: 4rpx;
	}

	.noCommodity {
		background-color: #fff;
		padding-bottom: 30rpx;
		margin-top: 90rpx;
	}
</style>